package com.ncu702.backend.controller;

import com.ncu702.backend.common.BaseResponse;
import com.ncu702.backend.common.ErrorCode;
import com.ncu702.backend.common.ResultUtils;
import com.ncu702.backend.model.dto.articles.ArticleVo;
import com.ncu702.backend.model.entity.Article;
import com.ncu702.backend.model.entity.User;
import com.ncu702.backend.model.untils.TokeConfig;
import com.ncu702.backend.service.ArticleService;
import com.ncu702.backend.service.RewardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/reward")
@Slf4j
public class RewardController {

    @Resource
    private RewardService rewardService;
    @Resource
    private TokeConfig tokeConfig;
    @Resource
    private ArticleService articleService;


    //展示所有悬赏信息，title和articleId即可
    @GetMapping("/all")
    public BaseResponse<List<ArticleVo>> getAllReward() {
        List<ArticleVo> article = rewardService.getAllReward();
        return ResultUtils.success(article);
    }

    //将文章改成悬赏，要求token拿到的uid和文章的uid相同才能设置悬赏
    @PostMapping("/add")
    public BaseResponse<Integer> addReward(
            @RequestParam String articleId,
            @RequestParam Integer money,
            HttpServletRequest request
    ) {
        //获取jwt
        String jwtToken = request.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if (!checkToke) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, "token失效");
        }

        User tokeMessage=tokeConfig.getTokeMessage(jwtToken);
        Long uid=tokeMessage.getUid();
        Long uuid=articleService.getArticleById(articleId).getUser().getUid();

        //自由自己能给自己悬赏
        if(!uid.equals(uuid)){
            return ResultUtils.error(ErrorCode.FORBIDDEN_ERROR, "you do not have permission to set reward!");
        }
        Integer addReward = rewardService.addReward(articleId,money);
        return ResultUtils.success(addReward);
    }



    //将文章取消悬赏
    //将文章改成悬赏，要求token拿到的uid和文章的uid相同才能设置悬赏
    @PostMapping("/delete")
    public BaseResponse<Integer> deleteReward(
            @RequestParam String articleId,
            HttpServletRequest request
    ) {
        //获取jwt
        String jwtToken = request.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if (!checkToke) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, "token失效");
        }

        User tokeMessage=tokeConfig.getTokeMessage(jwtToken);
        Long uid=tokeMessage.getUid();
        Long uuid=articleService.getArticleById(articleId).getUser().getUid();

        //自由自己能给自己悬赏
        if(!uid.equals(uuid)){
            return ResultUtils.error(ErrorCode.FORBIDDEN_ERROR, "you do not have permission to set reward!");
        }
        Integer addReward = rewardService.deleteReward(articleId);
        return ResultUtils.success(addReward);
    }


    //给某人分配赏金




}
